//
//  CNMyOrderTableViewController.m
//  BeeQuick_19
//
//  Created by clinging on 16/9/27.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNMyOrderTableViewController.h"
#import "CNMyOrderModel.h"
#import "CNMyOrderTableViewCell.h"
#import "MBProgressHUD.h"

static NSString *orderID = @"orderID";
@interface CNMyOrderTableViewController ()<UITableViewDelegate>

@end

@implementation CNMyOrderTableViewController

NSArray *_imgURL;
NSArray *_orderList;

MBProgressHUD *_hud;

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     
     order_goods  商品详情
     completion_time 时间
     textStatus 已完成
     buttons  发福利
 */
    [self loadOrderData];
    [self.tableView registerNib:[UINib nibWithNibName:@"CNMyOrderTableViewCell" bundle:nil] forCellReuseIdentifier:orderID];
    self.tableView.rowHeight = 140;
    _hud =  [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];

    
}
-(void)viewWillAppear:(BOOL)animated
{
    _hud.label.text = NSLocalizedString(@"正在加载数据中...", @"HUD loading title");
    

}
-(void)loadOrderData{
// 创建一个可变数组来存储参数
   NSMutableDictionary *param = [NSMutableDictionary dictionary];
   [param setValue:@"13" forKey:@"call"];
    [DSHTTPClient postUrlString:@"MyOrders.json.php" withParam:param withSuccessBlock:^(id data) {
        
        NSArray *dataArr = data[@"data"];
        NSMutableArray *TempM = [NSMutableArray array];
        
        NSMutableArray *urlArr = [NSMutableArray array];
        [dataArr enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            CNMyOrderModel *order = [CNMyOrderModel myOrderWithDict:obj];
            
            NSMutableArray *arrTemp = [NSMutableArray array];
            [TempM addObject:order];
            
            NSArray *goodsArr = obj[@"order_goods"];
            
            [goodsArr enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [obj enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    
                    NSString *imgURL = obj[@"img"];
                    [arrTemp addObject:imgURL];
                }];
                
            }];
            [urlArr addObject:arrTemp];
            
        }];
        _orderList = TempM.copy;
        
        _imgURL = urlArr.copy;
        [self.tableView reloadData];
        [_hud hideAnimated:YES];
        
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
    
    
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _orderList.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CNMyOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:orderID forIndexPath:indexPath];
    
    cell.dataArr = _imgURL[indexPath.row];
    CNMyOrderModel *orderModel = _orderList[indexPath.row];
    
    cell.orderModel = orderModel;
    
    
    
    return cell;
}


@end
